﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;

using Refix.Core.Properties;
using Refix.Core.Repository;

namespace Refix.Core.Commands
{
    public class Synchronize : Command
    {
        protected override string HelpText
        {
            get { return ResourceDispenser.GetString("SYNCHRONIZE_HELP"); }
        }

        public override bool Validate()
        {
            if (!base.Validate())
            {
                return false;
            }
            if (Options.Arguments.Count != 1)
            {
                OutputWriter.WriteLine(OutputVerbosity.Quiet, ResourceDispenser.GetString("WRONG_NUMBER_OF_ARGUMENTS"));
                ShowHelp();
                return false;
            }
            return true;
        }

        public override bool Execute()
        {
            string assemblyName = Options.Arguments[0];
            LocalRepository localRepository = GetLocalRepository();
            int localRevision = localRepository.GetComponentLocalRevision(assemblyName);
            int localRemoteRevision = localRepository.GetComponentRemoteRevision(assemblyName);
            int remoteRemoteRevision = localRepository.GetRemoteComponentRevision(assemblyName);
            if (remoteRemoteRevision == -1)
            {
                return false;
            }
            if (localRevision == -1)
            {
                using (OutputWriter.Highlight(ConsoleColor.Red))
                {
                    OutputWriter.WriteLine(OutputVerbosity.Quiet,
                                           ResourceDispenser.GetString("SYNCHRONIZE_DOES_NOT_EXIST_LOCALLY"),
                                           assemblyName);
                }
            }
            bool localIsModified = localRemoteRevision != localRevision;
            OutputWriter.WriteLine(OutputVerbosity.Normal,
                                   ResourceDispenser.GetString("SYNCHRONIZE_SUMMARY"),
                                   assemblyName, localRemoteRevision, remoteRemoteRevision,
                                   localIsModified
                                       ? ResourceDispenser.GetString("SYNCHRONIZE_MODIFIED")
                                       : ResourceDispenser.GetString("SYNCHRONIZE_UNMODIFIED"));
            if (localRemoteRevision == remoteRemoteRevision)
            {
                if (localIsModified)
                {
                    localRepository.SynchronizeWithRemoteRepository(assemblyName);
                    return true;
                }
                OutputWriter.WriteLine(ResourceDispenser.GetString("SYNCHRONIZE_NOT_REQUIRED"));
                return true;
            }

            var remoteRepository = new RemoteRepository(Configuration.RemoteRepository, localRepository.BaseDirectory);
            remoteRepository.DownloadAllVersions(assemblyName, ResourceDispenser.GetString("SYNCHRONIZE_SUCCESS"));

            return true;
        }
    }
}